package com.schoolCommunity.util;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.schoolCommunity.entity.Member;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Set;

@Component
public class CheckInsertMember {

    @Resource
    private com.schoolCommunity.mapper.MemberMapper MemberMapper;

    Logger log = Logger.getLogger(CheckInsertMember.class);


    //获取到要注册的member对象
    public String CheckInsertMember(Member Member) {

        //获取当前年份
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);

        //获取已经注册的邮箱
        Set<String> allMails = MemberMapper.getAllEmailsForSet();

        if (allMails.contains(Member.getEmail()))  //如果存在此邮，直接返回信息
            return "这个邮箱已经被注册";
        if (Member.getId() != null && Member.getId() != 0)   //检测伪造ID
        {
            log.info("获取到的用户id是" + Member.getId());
            return "用户注册不能指定ID，注册ID必须为空";
        }

        if (Member.getFamilyName() == null || Member.getFamilyName().length() > 4) {
            log.info("获取到的用户姓氏是" + Member.getFamilyName() + "为空或过长");
            return "用户注册姓氏不能为空，长度不能超过4个字符";
        }

        if (Member.getSex() != 1 && Member.getSex() != 0) {
            log.info("获取到的性别是号码是" + Member.getSex());
            return "用户性别必须为数字0或者数字1";
        }

        if (!Member.getEmail().matches("^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")) {
            log.info("获取到的用户注册邮箱是" + Member.getEmail());
            return "邮箱格式不正确";
        }

        if (Member.getEmail().length() >= 30) {
            log.info("获取到的用户注册邮箱是" + Member.getEmail() + "过长");
            return "邮箱字符长度不能超过30个字符";
        }

        if (Member.getPassword().length() > 16) {
            log.info("获取到用户注册的密码是" + Member.getPassword() + "过长");
            return "密码长度不能超过16个字符";
        }

        if (Member.getGrade() > year || Member.getGrade() < year - 4) {
            log.info("用户注册的年级不在校" + Member.getGrade());
            return "现阶段这个年级不在校";
        }

        if (Member.getNickname().length() > 20) {
            log.info("用户注册昵称过长");
            return "昵称请不要长于20个字符";
        }


        return "数据安全";

    }
}
